#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#define NAMEDPIPE_NAME "/tmp/my_named_pipe"


int fd =-1;
int len;

struct Data{
    int BUFSIZE;
    char buf[];
};

int create_file() {

    if ( mkfifo(NAMEDPIPE_NAME, 0777) ) {
        perror("mkfifo");
        return -1;
    }
    printf("%s is created\n", NAMEDPIPE_NAME);

    if ( (fd = open(NAMEDPIPE_NAME, O_RDONLY)) <= 0 ) {
        perror("open");
        return -1;
    }
    return fd;
}
void file_write(const char message[]){
    write(fd, &message, sizeof(message[0]));
    //write(&message, sizeof(message[0]), (sizeof(message)/sizeof(message[0])), fd);
}

void close_file(){
    perror("read");
    close(fd);
    remove(NAMEDPIPE_NAME);
}

struct Data read_file(char message[]){
    struct Data data;
    data.BUFSIZE = sizeof(message)/sizeof(message[0]);
    //data.buf[data.BUFSIZE];
    memset(message, '\0', data.BUFSIZE);
    read(fd, message, BUFSIZE-1);
    
    //return message;

}